package com.cl.short_video_java.mp;

import com.cl.short_video_java.common.Result;
import com.cl.short_video_java.config.mq.RabbitMqConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author 付
 * @Date 2024-11-30 21:20
 * @Version 1.0
 * @description
 * @since 1.0
 */

@Component
@Slf4j
public class VideoProducer {

	@Resource
	RabbitTemplate rabbitTemplate;

	/**
	 * 普通队列
	 *
	 * @param result
	 */
	public void sendJson(Result result) {
		log.info("消息发送-------------------------------{}", result);
		rabbitTemplate.convertAndSend(RabbitMqConfig.DIRECT_EXCHANGE, RabbitMqConfig.DIRECT_ROUTING_KEY, result);
	}

	/**
	 * 延时队列生产
	 *
	 * @param result
	 */
	public void sendDelayUserMap(Result result) {
		rabbitTemplate.convertAndSend(RabbitMqConfig.DELAY_EXCHANGE, RabbitMqConfig.DELAY_ROUTING_KEY, result, new MessagePostProcessor() {
			@Override
			public Message postProcessMessage(Message message) throws AmqpException {
				log.info("延时队列发送---------------------------{}", result);
				//消息延迟5秒
				message.getMessageProperties().setHeader("x-delay", 5000);
				return message;
			}
		});
	}
}
